package com.marlon.auto.service.impl;


import com.marlon.auto.common.base.service.ExcelBaseServiceImpl;
import com.marlon.auto.dao.AreaInfoDao;
import com.marlon.auto.entity.mapper.AreaInfoMapper;
import com.marlon.auto.entity.po.AreaInfoPO;
import com.marlon.auto.entity.vo.AreaInfoVO;
import com.marlon.auto.service.AreaInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author marlon
 * @since 2022-04-19
 */
@Service
@Transactional
public class AreaInfoServiceImpl extends ExcelBaseServiceImpl<AreaInfoPO, String> implements AreaInfoService {

    AreaInfoMapper mapper = AreaInfoMapper.INSTANCE;
    @Autowired
    private AreaInfoDao areaInfoDao;

    @Override
    public JpaRepositoryImplementation<AreaInfoPO, String> getDao() {
        return areaInfoDao;
    }

    @Override
    public Class getPOClass() {
        return AreaInfoPO.class;
    }

    @Override
    public Class getVOClass() {
        return AreaInfoPO.class;
    }

    @Override
    public Page<AreaInfoVO> findAllByVOAndKeyword(AreaInfoVO query, Pageable page) {
        if(query.getKeyword() == null){
            query.setKeyword("");
        }
        query.setKeyword(query.getKeyword().toLowerCase());
        Page<AreaInfoPO> result = areaInfoDao.findAllByVOAndKeyword(query,page);
        return new PageImpl<>(mapper.poToVO((result.getContent())),result.getPageable(),result.getTotalElements());
    }

    @Override
    public List<AreaInfoVO> findAllAvailable() {
        return mapper.poToVO(areaInfoDao.findAllAvailable());
    }
}